package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.service.BusinessBankService;
import com.api.shopapi.service.BusinessStaffRelService;
import com.api.shopapi.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * 商家的银行卡
 * */

@RestController
@RequestMapping("BusinessBank")
public class BusinessBankController extends BaseController{

    @Autowired
    @Qualifier("BusinessBankServiceImplOne")
    private BusinessBankService businessBankService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("BusinessStaffRelServiceImplOne")
    private BusinessStaffRelService businessStaffRelService;

    //添加
    @RequestMapping(value = "Add", method = RequestMethod.POST)
    @Transactional
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/BusinessBank/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            String bankName = jsonObject.getString("bankName");
            String bankBranch = jsonObject.getString("bankBranch");
            String bankNumber = jsonObject.getString("bankNumber");
            BusinessStaffRel businessStaffRel = businessStaffRelService.getByStaffCode(staffCode);
            BusinessBank businessBankByBusinessCode = businessBankService.getByBusinessCode(businessStaffRel.getBusinessCode());
            if(businessBankByBusinessCode!=null){
                return failed("只能添加一个银行卡！");
            }
            BusinessBank businessBank=new BusinessBank();
            businessBank.setBusinessBankCode(UUID.randomUUID().toString());
            businessBank.setBusinessCode(businessStaffRel.getBusinessCode());
            businessBank.setBankName(bankName);
            businessBank.setBankBranch(bankBranch);
            businessBank.setBankNumber(bankNumber);
            businessBank.setStatus(1);
            businessBank.setCreateTime(new Date());

            businessBankService.insert(businessBank);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
//            e.printStackTrace();
            insertError("/BusinessBank/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //编辑
    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    @Transactional
    public MyResult Edit(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/BusinessBank/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            String businessBankCode = jsonObject.getString("businessBankCode");
            String bankName = jsonObject.getString("bankName");
            String bankBranch = jsonObject.getString("bankBranch");
            String bankNumber = jsonObject.getString("bankNumber");
            int status = jsonObject.getIntValue("status");
            BusinessBank businessBank = businessBankService.getByBusinessBankCode(businessBankCode);
            businessBank.setStatus(status);
            businessBank.setBankNumber(bankNumber);
            businessBank.setBankBranch(bankBranch);
            businessBank.setBankName(bankName);
            businessBankService.update(businessBank);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/BusinessBank/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //根据staffCode查询银行卡信息
    @RequestMapping(value = "getByBusinessInfo",method = RequestMethod.POST)
    public   MyResult getByBusinessInfo(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/BusinessBank/getByBusinessInfo");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            BusinessStaffRel businessStaffRel = businessStaffRelService.getByStaffCode(staffCode);
            if(businessStaffRel==null){
                return failed("此账号未关联商家或者不是商家账号！");
            }
            BusinessBank businessBank = businessBankService.getByBusinessCode(businessStaffRel.getBusinessCode());
            return success(businessBank);
        } catch (Exception e) {
//            e.printStackTrace();
            insertError("/BusinessBank/getByBusinessInfo",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //根据BusinessCode查询银行卡信息
    @RequestMapping(value = "getByBusiness",method = RequestMethod.POST)
    public   MyResult getByBusiness(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String businessCode = jsonObject.getString("businessCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/BusinessBank/getByBusiness");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            BusinessBank businessBank = businessBankService.getByBusinessCode(businessCode);
            return success(businessBank);
        } catch (Exception e) {
//            e.printStackTrace();
            insertError("/BusinessBank/getByBusiness",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }
}
